<template>
  <div>
    <p>
      <button @click="add()" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-edit"></i>
        新增
      </button>
      &nbsp;
      <button @click="list(1)" class="btn btn-white btn-default btn-round">
        <i class="ace-icon fa fa-refresh"></i>
        刷新
      </button>
    </p>

    <pagination ref="pagination" v-bind:list="list" v-bind:itemCount="8"></pagination>

    <table id="simple-table" class="table  table-bordered table-hover">
      <thead>
      <tr>
        <th>全局唯一标识符</th>
        <th>组织id（is_admin字段为1时，此字段为必填）</th>
        <th>用户名</th>
        <th>邮箱</th>
        <th>是否启用 true是   false否</th>
        <th>最后登录IP</th>
        <th>密码</th>
        <th>登录错误次数</th>
        <th>登录次数</th>
        <th>最后登录时间</th>
        <th>用户组</th>
        <th>用户等级</th>
        <th>积分</th>
        <th>手机号</th>
        <th>密码最后被改变时间</th>
        <th>是否管理员</th>
        <th>是否已重置密码</th>
        <th>提醒修改密码消息是否已发</th>
        <th>创建时间</th>
        <th>修改时间</th>
        <th>逻辑删除标识。仅且仅有0和1两个值，1表示已经被逻辑删除，0表示正常可用。</th>
        <th>操作</th>
      </tr>
      </thead>

      <tbody>
      <tr v-for="user in users">
        <td>{{user.id}}</td>
        <td>{{user.relationId}}</td>
        <td>{{user.username}}</td>
        <td>{{user.userEmail}}</td>
        <td>{{user.isEnabled}}</td>
        <td>{{user.lastLoginIp}}</td>
        <td>{{user.password}}</td>
        <td>{{user.loginErrorCount}}</td>
        <td>{{user.loginCount}}</td>
        <td>{{user.lastLoginTime}}</td>
        <td>{{user.groupId}}</td>
        <td>{{user.levelId}}</td>
        <td>{{user.integral}}</td>
        <td>{{user.usePhone}}</td>
        <td>{{user.lastPasswordChange}}</td>
        <td>{{user.isAdmin}}</td>
        <td>{{user.isResetPassword}}</td>
        <td>{{user.passMsgHasSend}}</td>
        <td>{{user.createTime}}</td>
        <td>{{user.updateTime}}</td>
        <td>{{user.deletedFlag}}</td>
      <td>
        <div class="hidden-sm hidden-xs btn-group">
          <button @click="edit(user)" class="btn btn-xs btn-info">
            <i class="ace-icon fa fa-pencil bigger-120"></i>
          </button>
          <button @click="del(user.id)" class="btn btn-xs btn-danger">
            <i class="ace-icon fa fa-trash-o bigger-120"></i>
          </button>
        </div>
      </td>
      </tr>
      </tbody>
    </table>

    <div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">表单</h4>
          </div>
          <div class="modal-body">
            <form class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label">组织id（is_admin字段为1时，此字段为必填）</label>
                <div class="col-sm-10">
                  <input v-model="user.relationId" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                  <input v-model="user.username" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">邮箱</label>
                <div class="col-sm-10">
                  <input v-model="user.userEmail" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">是否启用 true是   false否</label>
                <div class="col-sm-10">
                  <input v-model="user.isEnabled" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">最后登录IP</label>
                <div class="col-sm-10">
                  <input v-model="user.lastLoginIp" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                  <input v-model="user.password" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">登录错误次数</label>
                <div class="col-sm-10">
                  <input v-model="user.loginErrorCount" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">登录次数</label>
                <div class="col-sm-10">
                  <input v-model="user.loginCount" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">最后登录时间</label>
                <div class="col-sm-10">
                  <input v-model="user.lastLoginTime" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">用户组</label>
                <div class="col-sm-10">
                  <input v-model="user.groupId" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">用户等级</label>
                <div class="col-sm-10">
                  <input v-model="user.levelId" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">积分</label>
                <div class="col-sm-10">
                  <input v-model="user.integral" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">手机号</label>
                <div class="col-sm-10">
                  <input v-model="user.usePhone" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">密码最后被改变时间</label>
                <div class="col-sm-10">
                  <input v-model="user.lastPasswordChange" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">是否管理员</label>
                <div class="col-sm-10">
                  <input v-model="user.isAdmin" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">是否已重置密码</label>
                <div class="col-sm-10">
                  <input v-model="user.isResetPassword" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">提醒修改密码消息是否已发</label>
                <div class="col-sm-10">
                  <input v-model="user.passMsgHasSend" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">创建时间</label>
                <div class="col-sm-10">
                  <input v-model="user.createTime" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">修改时间</label>
                <div class="col-sm-10">
                  <input v-model="user.updateTime" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">逻辑删除标识。仅且仅有0和1两个值，1表示已经被逻辑删除，0表示正常可用。</label>
                <div class="col-sm-10">
                  <input v-model="user.deletedFlag" class="form-control">
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button @click="save()" type="button" class="btn btn-primary">保存</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
  </div>
</template>

<script>
  import Pagination from "../../components/pagination";
  export default {
    components: {Pagination},
    name: "managerlogin-user",
    data: function() {
      return {
        user: {},
        users: [],
      }
    },
    mounted: function() {
      let _this = this;
      _this.$refs.pagination.size = 5;
      _this.list(1);
      // sidebar激活样式方法一
      // this.$parent.activeSidebar("managerlogin-user-sidebar");

    },
    methods: {
      /**
       * 点击【新增】
       */
      add() {
        let _this = this;
        _this.user = {};
        $("#form-modal").modal("show");
      },

      /**
       * 点击【编辑】
       */
      edit(user) {
        let _this = this;
        _this.user = $.extend({}, user);
        $("#form-modal").modal("show");
      },

      /**
       * 列表查询
       */
      list(page) {
        let _this = this;
        Loading.show();
        _this.$ajax.post(process.env.VUE_APP_SERVER + '/managerlogin/admin/user/list', {
          page: page,
          size: _this.$refs.pagination.size,
        }).then((response)=>{
          Loading.hide();
          let resp = response.data;
          _this.users = resp.content.list;
          _this.$refs.pagination.render(page, resp.content.total);

        })
      },

      /**
       * 点击【保存】
       */
      save() {
        let _this = this;

        // 保存校验
        if (1 != 1
          || !Validator.length(_this.user.relationId, "组织id（is_admin字段为1时，此字段为必填）", 1, 25)
          || !Validator.require(_this.user.username, "用户名")
          || !Validator.length(_this.user.username, "用户名", 1, 150)
          || !Validator.length(_this.user.userEmail, "邮箱", 1, 60)
          || !Validator.require(_this.user.isEnabled, "是否启用 true是   false否")
          || !Validator.length(_this.user.lastLoginIp, "最后登录IP", 1, 50)
          || !Validator.require(_this.user.password, "密码")
          || !Validator.length(_this.user.password, "密码", 1, 50)
          || !Validator.require(_this.user.loginErrorCount, "登录错误次数")
          || !Validator.length(_this.user.usePhone, "手机号", 1, 11)
          || !Validator.require(_this.user.isAdmin, "是否管理员")
          || !Validator.require(_this.user.isResetPassword, "是否已重置密码")
          || !Validator.require(_this.user.passMsgHasSend, "提醒修改密码消息是否已发")
          || !Validator.require(_this.user.createTime, "创建时间")
          || !Validator.require(_this.user.deletedFlag, "逻辑删除标识。仅且仅有0和1两个值，1表示已经被逻辑删除，0表示正常可用。")
        ) {
          return;
        }

        Loading.show();
        _this.$ajax.post(process.env.VUE_APP_SERVER + '/managerlogin/admin/user/save', _this.user).then((response)=>{
          Loading.hide();
          let resp = response.data;
          if (resp.success) {
            $("#form-modal").modal("hide");
            _this.list(1);
            Toast.success("保存成功！");
          } else {
            Toast.warning(resp.message)
          }
        })
      },

      /**
       * 点击【删除】
       */
      del(id) {
        let _this = this;
        Confirm.show("删除用户信息主体表后不可恢复，确认删除？", function () {
          Loading.show();
          _this.$ajax.delete(process.env.VUE_APP_SERVER + '/managerlogin/admin/user/delete/' + id).then((response)=>{
            Loading.hide();
            let resp = response.data;
            if (resp.success) {
              _this.list(1);
              Toast.success("删除成功！");
            }
          })
        });
      }
    }
  }
</script>